<?php

namespace app\api\controller;

use app\common\lib\Arr;
use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;
use think\response\Json;

class House extends AuthBase
{

    //小区列表
    public function getHouseList(){
        $longitude = $this->request->param("longitude", "", "trim");
        $latitude = $this->request->param("latitude", "", "trim");

        $map = [
            ['pid','=',0]
        ];
        $houseBis = new \app\common\business\House();
        $list = $houseBis->getListByWhereWithOrder($map,0,0,'sort desc,id desc','id,name,longitude,latitude');

        if (!empty($list)) {
            //根据经纬度计算距离
            $data = [
                'longitude'=>$longitude,
                'latitude'=>$latitude
            ];
            $list = $houseBis->handlerListByOrder($list,$data);//根据order返回不同list
        }

        return Show::success($list);
    }

    //根据小区id获取楼、单元、户树状
    public function getHouseTree(){
        $id = input("id", 0, "intval");

        try {
            $data = (new \app\common\business\House())->getCategoryByPid($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }

        $result = Arr::getTree($data);
        return Show::success($result);
    }
}
